package com.itdfq.rabbitmq.reveiver;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RabbitListener(queues = "queue2")
public class DictReceive implements ChannelAwareMessageListener {

   /* @RabbitHandler
    public void pros(Map message) {
        System.out.printf("DirectReceiver消费者收到消息  : " + message.toString());
    }*/

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            System.out.println("Receiver>>>>>>>>消息已消费");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            System.out.println("Receiver>>>>>>拒绝消息，要求MQ重新发送");
            e.printStackTrace();
            throw e;
        }
    }
}
